package com.chongjing.web.controller.chat;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chongjing.chat.domain.MessageConditionData;
import com.chongjing.chat.domain.MessageResponse;
import com.chongjing.common.annotation.DataSource;
import com.chongjing.common.enums.DataSourceType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.chongjing.common.annotation.Log;
import com.chongjing.common.core.controller.BaseController;
import com.chongjing.common.core.domain.AjaxResult;
import com.chongjing.common.enums.BusinessType;
import com.chongjing.chat.service.IMessageService;
import com.chongjing.common.utils.poi.ExcelUtil;
import com.chongjing.common.core.page.TableDataInfo;

/**
 * 消息Controller
 *
 * @author zhoutianyu
 * @date 2025-02-05
 */
@Slf4j
@RestController
@RequestMapping("/chat")
@DataSource(DataSourceType.SLAVE) //这样，执行该方法的时候就会选择SLAVE数据源
public class MessageController extends BaseController
{
    @Autowired
    private IMessageService iMessageService;

    /**
     * 分页查询消息列表
     */
    @PreAuthorize("@ss.hasPermi('chat:message:list')")
    @GetMapping("/page")
    public TableDataInfo page(MessageConditionData messageConditionData)
    {
        log.info("查询条件：{}",messageConditionData);
        startPage();
        List<MessageResponse> list = iMessageService.page(messageConditionData);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('chat:message:list')")
    @GetMapping("/page/{id}")
    public AjaxResult getById(@PathVariable("id") Long id){
        log.info("根据消息id查询消息：{}",id);
        return success(iMessageService.getById(id));
    }

    /**
     * 删除信息
     *
     */
    @DeleteMapping("/deleteMessage")
    @PreAuthorize("@ss.hasPermi('chat:message:list')")
    @Log(title = "消息管理", businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestBody Map<String,String> request) {
        String ids = request.get("ids");
        iMessageService.deleteMessage(ids);
        return AjaxResult.success("成功删除消息");
    }

    /**
     * 导出信息列表
     */
    @PreAuthorize("@ss.hasPermi('chat:message:export')")
    @Log(title = "导出消息列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageConditionData messageConditionData)
    {
        List<MessageResponse> list = iMessageService.page(messageConditionData);
        ExcelUtil<MessageResponse> util = new ExcelUtil<MessageResponse>(MessageResponse.class);
        util.exportExcel(response, list, "消息列表数据");
    }

}
